package com.ymatou.iapi.activitymanage.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.map.HashedMap;

import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.ModifyLiveActivityBean;
import com.ymatou.iapi.liveseller.service.ModifyLiveActivityCall;
import com.ymatou.iapi.optrade.parameter.enums.CouponApplianceTypeEnum;
import com.ymatou.iapi.optrade.parameter.vo.CouponVo;
import com.ymatou.iapi.productmanager.parameter.SetPspStatusBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.SetPspStatusCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.YMTTrading.TradingAllPromotion;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;



public class activityData {

	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	
	/**
	 * 创建多规格商品参加活动
	 * @return
	 * @throws Exception
	 */
	public static HashedMap createProductInActivity() throws Exception{
		
		AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
		// 设置商品库存,规格库存
		Integer catalogNum = 1000;
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
		
		//获取商品ID
		String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
		//获取规格ID
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		//如果是单规格，去掉规格列表的第一个参数#################################
		//String catalog0 = catalogs.remove(0);
		//创建一个活动  得到活动id
		Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
		
		//报名活动  获得商品在活动中的id
		List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
		//审核通过
		ProductManagerCallServiceV2.approveProductInActivityV3(productId,aids.get(0),true,300);	
		
		//需要其他返回字段，可在HashedMap中追加
		HashedMap hashedMap=new HashedMap();
		hashedMap.put("productId", productId);
		hashedMap.put("activityId", activityId);
		return hashedMap;
		
	}
	
	/*
	 * 商品参加多活动促销 1：普通活动 2：直播商品秒杀活动
	 */
	public static HashedMap createProductInMoreActivity() throws Exception 
	{
		 ModifyLiveActivityBean modifyliveactivityBean=new ModifyLiveActivityBean();
		 ModifyLiveActivityCall modifyliveactivityCall=new ModifyLiveActivityCall();

		 YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
		 
		AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
		// 设置商品库存,规格库存
		Integer catalogNum = 1000;
		addpro.getProduct().getCatalogList().forEach(catalog -> {
			catalog.setStock(catalogNum);
		});
		
		//获取商品ID
		String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
        //创建商品同步指令
		//获取规格ID
		List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		//如果是单规格，去掉规格列表的第一个参数
		//String catalog0 = catalogs.remove(0);
		Logger.comment("商品参加普通活动开始#########################");
		//创建一个活动  得到活动id
		Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
		
		//报名活动  获得商品在活动中的id
		List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
		//审核通过
		ProductManagerCallServiceV2.approveProductInActivityV3(productId,aids.get(0),true,300);	
		Logger.comment("商品参加普通结束#########################");
		Logger.comment("商品参加直播活动开始#########################");
		Logger.comment("创建直播");
		// 5分钟后开始200分钟后结束
		CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
		Date now = YMTDateUtil.getDate();
		//半小时之内会锁定不能修改
		Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
		Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
		clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
		
		LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
		Logger.comment("加入商品");
		LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
		Logger.comment("查询商品规格");

		List<YmtCatalogsI> catalogsList = CIWapper.selectCatalogsByProductId(productId);
		List<String> cids = catalogsList.stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
		Logger.comment("创建秒杀活动");
		CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
		LiveRoomCallService.CreateLiveActivity(createliveactivityBean);	
		
		modifyliveactivityBean=LiveRoomCallService.initModifyLiveActivityBean(createliveactivityBean);
		//修改结束时间
		modifyliveactivityBean.getLiveActivity().setEndTime(String.valueOf(new Date(now.getTime() + 201 * 60 * 1000).getTime()));
		modifyliveactivityCall.setData(modifyliveactivityBean);
		modifyliveactivityCall.callService();
		Logger.comment("商品参加直播活动结束#########################");
		
		//需要其他返回字段，可在HashedMap中追加
		HashedMap hashedMap=new HashedMap();
		hashedMap.put("productId", productId);
		hashedMap.put("activityId", activityId);
		hashedMap.put("createliveId", createlive.getId());
		return hashedMap;
	}
	/**
	 * 商品绑定买手优惠券
	 * @param productId
	 * @return
	 */
	public CouponVo productBindSellerCoupon(String productId,Integer sellerId){
		
		int userId=TestDataManager.getRandomIntNum(5);
		CouponVo sellerCoupon1 = TradingAllPromotion.createSellerCouponVo(
				(long)userId, (long)sellerId, new BigDecimal(30), BigDecimal.TEN);

		sellerCoupon1.setApplianceType(CouponApplianceTypeEnum.SPEC_RODUCT);

		sellerCoupon1.setApplianceTargets(Arrays.asList(productId));

        return sellerCoupon1;
        
	}
	
	/**
	 * 商品绑定平台优惠券
	 * @param productId
	 * @return
	 */
	public CouponVo productBindPlatformCoupon(String productId,Integer sellerId){
		
		int userId=TestDataManager.getRandomIntNum(5);
		CouponVo sellerCoupon1 = TradingAllPromotion.createSellerCouponVo(
				(long)userId, (long)sellerId, new BigDecimal(30), BigDecimal.TEN);

		sellerCoupon1.setApplianceType(CouponApplianceTypeEnum.SPEC_RODUCT);

		sellerCoupon1.setApplianceTargets(Arrays.asList(productId));

        return sellerCoupon1;
        
	}
	
	/**
	 * 是否是pst商品  isPsp=true：是pst商品   isPsp=false:不是psp商品
	 * @param isPsp
	 * @throws Exception
	 */
	public String isPspProduct(boolean isPsp) throws Exception{
		
		int sellerId=Integer.valueOf(EnvSetup.getData("productpriceSellerId"));
		AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
		//获取商品ID
		String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
		
		IntergratedProductMapper ipm = SqlMapperFactory
                .getIntergratedProductMapper();
        ipm.insertProductInfo(productId,sellerId);
        if(isPsp==true){
			new YmtProductsIWapper().updatePspStatus(productId, 3);
			
			SetPspStatusBean setpspstatusBean = new SetPspStatusBean();
			SetPspStatusCall setpspstatusCall = new SetPspStatusCall();
			
			setpspstatusBean.setIsPassViolate(true);
			setpspstatusBean.setIsRemovePsp(false);
			setpspstatusBean.setProductId(productId);
			setpspstatusBean.setSellerId(sellerId);
			setpspstatusCall.setData(setpspstatusBean);
			setpspstatusCall.callService();	
        }
        return productId;
	}
	public static void main(String[] args) throws Exception
	{
		//HashedMap hashedMap=createProductInActivity();
		HashedMap hashedMap=createProductInMoreActivity();
		System.out.println("hashedMap:"+hashedMap);
		
	}
}
